package com.example.mybaseapp.widget.circle_seekbar

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.PointF
import android.graphics.Shader
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.mybaseapp.R
import kotlin.properties.Delegates

/**
 * 刻度盘主要类，自定义控件
 */
class ACircleSeekBar:View {
    /**点的数据*/
    val datas = mutableListOf<PointsData>()
    /**圆心坐标*/
    private val center:PointF = PointF()
    /**圆心半径*/
    private var radius=0F
    /**圆的背景画笔*/
    private val circlePaint = Paint()
    private val circleStrokePaint = Paint()
    private val drawaUIUtil = DrawaUIUtil(context)
    private val eventController = EventController(drawaUIUtil)

    private var bgColor = Color.BLACK
    private var bgStrokeColor = Color.BLACK
    /**指针的画笔*/
    private val cursorCirclePaint = Paint()
    /**指针上小圆的画笔*/
    private val cursorSmallCirclePaint = Paint()
    /**指针的背景白色*/
    private var cursorBgColor = Color.WHITE
    /**小圆的颜色*/
    private var cursorSmallBgColor = Color.RED
    /**刻度的点*/
    private val scaleDotPaint = Paint()
    private var scaleDotColor = Color.WHITE
    /**大的刻度*/
    private val scaleBigPaint = Paint()
    /**文字刻度*/
    private val scaleTextPaint = Paint()
    /**文字刻度 文字大小*/
    private var scaleTextSize = 8F
    /**大刻度的颜色*/
    private var scaleBigColor = Color.WHITE
    /**当前点击的坐标*/
    private var currentTouchPoint = PointF()
    /**当前移动的坐标*/
    private var currentMovePoint = PointF()
    /**指针点击*/
    private var CURSOR_DOWN = false
    /**指针移动*/
    private var CURSOR_MOVE = false
    /**指针放手*/
    private var CURSOR_UP = false
    /**移动的角度*/
    private var moveDegree = 0F

    private var shader:LinearGradient by Delegates.notNull()
    private var strokeStartColor = Color.BLACK
    private var strokeMiddleColor = Color.BLACK
    private var strokeEndColor = Color.BLACK

    constructor(context: Context) : super(context) {
        initAttars(null)
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initAttars(attrs)
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        // 构造函数处理，处理从 XML 布局文件中传入的属性和样式
        initAttars(attrs)
        initView()
    }

    /**
     * 设置监听
     */
    fun addSelectListener(selectListener: SelectListener){
        eventController.selectListener = selectListener
    }

    fun addPoints(mPointsData: MutableList<PointsData>){
        datas.clear()
        datas.addAll(mPointsData)
        drawaUIUtil.refreshData(datas)
        invalidate()
    }

    fun addIntPoints(mPoints: List<Int>){
        datas.clear()
        for(d in mPoints){
            datas.add(PointsData(d))
        }
        drawaUIUtil.refreshData(datas)
        invalidate()
    }

    private fun initAttars(attrs: AttributeSet?) {
        attrs?.let{
            val typedArray = context.obtainStyledAttributes(it, R.styleable.ACircleSeekBar)
            bgColor = typedArray.getColor(R.styleable.ACircleSeekBar_circleBgColor, resources.getColor(R.color.circle_seekbar_bg,null))
            bgStrokeColor = typedArray.getColor(R.styleable.ACircleSeekBar_circleBgStrokeColor, resources.getColor(R.color.circle_seekbar_stroker_color,null))
            cursorBgColor = typedArray.getColor(R.styleable.ACircleSeekBar_cursorCircleColor, resources.getColor(R.color.cursor_circle_bg,null))
            cursorSmallBgColor = typedArray.getColor(R.styleable.ACircleSeekBar_cursorSmallCircleColor, resources.getColor(R.color.cursor_small_bg,null))
            scaleDotColor = typedArray.getColor(R.styleable.ACircleSeekBar_scaleDotColor, resources.getColor(R.color.circle_dot_color,null))
            scaleBigColor = typedArray.getColor(R.styleable.ACircleSeekBar_scaleBigColor, resources.getColor(R.color.circle_big_color,null))
            scaleTextSize = typedArray.getDimension(R.styleable.ACircleSeekBar_scaleBigColor, resources.getDimension(R.dimen.sp_8))
            strokeStartColor = typedArray.getColor(R.styleable.ACircleSeekBar_strokeStartColor, bgStrokeColor)
            strokeMiddleColor = typedArray.getColor(R.styleable.ACircleSeekBar_strokeMiddleColor, bgStrokeColor)
            strokeEndColor = typedArray.getColor(R.styleable.ACircleSeekBar_strokeEndColor, bgStrokeColor)
            typedArray.recycle()
        }
    }

    private fun initView() {
        circlePaint.apply {
            color = bgColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
        }
        circleStrokePaint.apply {
            color = bgStrokeColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
        }
        cursorCirclePaint.apply {
            color = cursorBgColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
        }
        cursorSmallCirclePaint.apply {
            color = cursorSmallBgColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
        }
        scaleDotPaint.apply {
            color = scaleDotColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            strokeCap = Paint.Cap.ROUND // 点的形状为圆形
        }
        scaleBigPaint.apply {
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = scaleBigColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
        }
        scaleTextPaint.apply {
            textSize = scaleTextSize
            color = scaleBigColor
            style = Paint.Style.FILL
            isAntiAlias = true//抗锯齿
            textAlign = Paint.Align.CENTER // 设置文本对齐方式为居中
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
//        val centerX = width / 2f
//        val centerY = height / 2f
//        val radius = width / 3f

        shader = LinearGradient(
            0f, 0f, width.toFloat(), 0f,
            intArrayOf(strokeStartColor, strokeMiddleColor, strokeEndColor),
            null,
            Shader.TileMode.CLAMP
        )
        circleStrokePaint.shader = shader
        println("onSizeChanged")
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        // 选择宽高中较小的值作为半圆形的半径
        radius = if (width < height) width / 2f else height / 2f
        center.x = width / 2f
        center.y = height.toFloat()
        drawaUIUtil.circleCenter = center
        drawaUIUtil.circleRadius = radius


    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawaUIUtil.drawCircleBg(canvas, center, radius, circlePaint,circleStrokePaint)
        drawaUIUtil.drawSmallCircle(canvas,center,radius, cursorCirclePaint,cursorSmallCirclePaint)
//        drawaUIUtil.drawScaleSmallDot(canvas,)
        drawaUIUtil.drawScale(canvas, datas, scaleBigPaint, scaleTextPaint, scaleDotPaint)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN->{
                currentTouchPoint.set(event.x,event.y)
                println("当前旋转的角度：按下一次")
//                CURSOR_DOWN = eventController.isTouchCursor(currentTouchPoint,drawaUIUtil.currentCursorPoint)
            }
            MotionEvent.ACTION_MOVE,MotionEvent.ACTION_UP->{
//                if(CURSOR_DOWN){
                    CURSOR_MOVE = true
                    currentMovePoint.set(event.x,event.y)

                moveDegree = eventController.moveDegree(currentTouchPoint,currentMovePoint)
                println("当前旋转的角度：onTouchEvent $moveDegree")
                moveDegree = eventController.moveAdjust(moveDegree,datas)
//                    eventController.moveCursor(moveDegree)
                    eventController.moveScale(moveDegree)
                if(event.action==MotionEvent.ACTION_UP){
                    eventController.upScale()
                    eventController.currentSelect(datas)
                    println("当前旋转的角度：放手显示")
                }
                    invalidate()
            }
        }
        return true
    }
}